package com.test.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class FileUploadUtil {

	private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

	/**
	 * 文件流拷贝
	 * 
	 * @param input
	 * @param output
	 * @return
	 * @throws IOException
	 */
	public static int copy(InputStream input, OutputStream output) throws IOException {
		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
		int count = 0;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}

	/**
	 * 将文件从一个文件夹保存到另一个文件夹 并保留层次顺序
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("unused")
	public static boolean generateFile(String oldPath, String filePath, String newFloder) throws Exception {

		filePath = filePath.replace("\\", "/");
		filePath = (filePath).replace("%20", " ");
		File oldFile = new File(filePath);
		if (!oldFile.exists()) {
			return false;
		}
		if (oldFile.isFile()) {
			String newFilePath = newFloder + "/" + filePath.substring(oldPath.length());
			File newFile = new File(newFilePath);
			if (!newFile.getParentFile().exists()) {
				newFile.getParentFile().mkdirs();
			}
			write(oldFile, newFile, false);
		} else {
			File[] oldFiles = oldFile.listFiles();
			int oldFilesSize = oldFiles.length;
			for (int i = 0; i < oldFilesSize; i++) {
				generateFile(oldPath, oldFiles[i].getAbsolutePath(), newFloder);
			}
		}
		return true;
	}

	/**
	 * 获取一个文件对象 如果文件对象不存在则新建文件返回
	 */
	public static File getFile(String filePath) throws IOException {

		if (filePath == null) {
			return null;
		}

		File file = new File(filePath);
		if (!file.exists()) {
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			file.createNewFile();
		}
		return file;

	}

	/**
	 * 保存一个文件
	 * 
	 * @throws IOException
	 */
	public static boolean saveFile(String filePath, byte[] b) throws IOException {

		File file = getFile(filePath);

		if (file == null) {
			return false;
		}

		OutputStream fos = new FileOutputStream(file);
		fos.write(b);
		fos.close();

		return true;
	}

	public static boolean saveFile(String filePath, InputStream in) throws IOException {

		// if( filePath == null ) return false;
		//		
		// OutputStream fos = new FileOutputStream( filePath );
		// byte[] b = upFile.getFileData();
		// fos.write(b);
		// fos.close();

		return true;
	}

	/**
	 * 保存一个文件
	 * 
	 * @throws IOException
	 */
	public static boolean saveFile(String filePath, String content) throws IOException {
		File file = getFile(filePath);

		if (file == null) {
			return false;
		}
		Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");

		writer.write(content);

		writer.flush();

		writer.close();

		return true;
	}

	public static void write(File src, File dest, boolean rename) throws Exception {
		if (!rename || (rename && !src.renameTo(dest))) {
			BufferedInputStream in = null;
			BufferedOutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dest));
				copy(in, out);
			}
			finally {
				if (in != null) {
					try {
						in.close();
					}
					catch (IOException e) {
						// ignore
					}
				}
				if (out != null) {
					try {
						out.close();
					}
					catch (IOException e) {
						// ignore
					}
				}
			}
		}
	}
}